package com.one.consumer.main.ui;


import android.content.Intent;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.CMemoryData;
import com.one.common.config.RouterPath;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.ResourceUtils;
import com.one.common.view.base.BaseFragment;
import com.one.common.view.base.BaseFragmentActivity;
import com.one.common.view.bottombar.BottomBarBean;
import com.one.common.view.bottombar.BottomBarView;
import com.one.common.view.bottombar.OnBottomBarSelectListener;
import com.one.consumer.R;
import com.one.consumer.main.ui.fragment.HomeFragment;
import com.one.consumer.main.ui.fragment.MyFragment;
import com.one.consumer.main.ui.fragment.OrderFragment;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RouterPath.MAIN_PAGE)
public class MainActivity extends BaseFragmentActivity {

    public static String TIPS = "";
    public static String CANKAO = "";


    @BindView(R.id.frame_content)
    FrameLayout frameContent;
    @BindView(R.id.bbv)
    BottomBarView bbv;

    @Override
    protected void initPresenter() {
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_main;
    }


    @Override
    protected int getContainerViewId() {
        return R.id.frame_content;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        noHasTitle();
    }

    @Override
    public void initView() {

        super.initView();
        initBottom();
        loadMainInfo();
    }

    /**
     * 初始化底部布局
     */
    private void initBottom() {
        List<BottomBarBean> bottomBarBeans = new ArrayList<>();

        BottomBarBean bean1 = new BottomBarBean();
        bean1.setText(ResourceUtils.getString(R.string.index_home));
        bean1.setDefaultIcon(R.mipmap.ic_index_home);
        bean1.setDefaultSelectIcon(R.mipmap.ic_index_home_check);

        BottomBarBean bean2 = new BottomBarBean();
        bean2.setText(ResourceUtils.getString(R.string.index_order));
        bean2.setDefaultIcon(R.mipmap.ic_index_order);
        bean2.setDefaultSelectIcon(R.mipmap.ic_index_order_check);

        BottomBarBean bean3 = new BottomBarBean();
        bean3.setText(ResourceUtils.getString(R.string.index_my));
        bean3.setDefaultIcon(R.mipmap.ic_index_my);
        bean3.setDefaultSelectIcon(R.mipmap.ic_index_my_check);

        bottomBarBeans.add(bean1);
        bottomBarBeans.add(bean2);
        bottomBarBeans.add(bean3);

        bbv.bindData(bottomBarBeans);
        bbv.bindTopView(frameContent);
        bbv.setTextSelectorLocalColor(R.color.text_color_b4, R.color.text_color_black_33);

        bbv.setOnBottomBarSelectListener(new OnBottomBarSelectListener() {
            @Override
            public void onSelect(int index, BottomBarBean bean) {
                changePage(index);
            }
        });

        changePage(0);
    }

    private void loadMainInfo(){
        LogUtil.e("loadMainInfo");
        RequestParams params = new RequestParams("https://yinlong.yinweish.com/kmCouponController/v1.0/findMaintailInfo");
        UserInfoBean bean = CMemoryData.getUserInfo();
        params.addBodyParameter("member_account",bean.getMember_account());
        params.addBodyParameter("member_id", bean.getMember_id()+"");
        params.addBodyParameter("member_token", bean.getMember_token());
        params.setCacheMaxAge(0);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.e("loadMainInfo onSuccess "+result);
                try {
                    JSONObject dataObject = new JSONObject(result).optJSONObject("data");
                    TIPS = dataObject.optString("info");
                    CANKAO = dataObject.optString("url");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.e("loadMainInfo onError "+ex.getMessage());
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(Callback.CancelledException cex) {}
            @Override
            public void onFinished() {}
        });
    }

    @Override
    protected ArrayList<BaseFragment> getFragments() {
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new OrderFragment());
        fragments.add(new MyFragment());

        return fragments;
    }

    @Override
    public void initData() {
        super.initData();

    }

    @Override
    public void changePage(int index) {
        super.changePage(index);
        bbv.setCheck(index);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        DefaultExtra extra = (DefaultExtra) intent.getSerializableExtra(DefaultExtra.getExtraName());
        if(extra!=null){
            changePage(extra.getNum());
        }
    }
}
